package com.sanss.trapParse;

import java.util.*;
import java.io.*;

import org.dom4j.io.*;
import org.dom4j.*;

import com.sanss.trapParse.info.RuleInfo;

public class Xml2Rules {

	private String mibName;// file to parse to rules without ".xml" extension

	private String xmlFile;

	private String outputFile;// output rules file

	private FileOutputStream fos;

	private File file;

	public Xml2Rules(String mibDir, String ruleDir, String mibName) {
		this.mibName = mibName;
		this.xmlFile = mibDir + "/" + mibName + ".xml";
		this.outputFile = ruleDir + "/" + mibName + ".rules";
	}

	public void NotificationParser() {
		try {
			createFile();// create rules file

			// read XML mib file
			FileInputStream fis = new FileInputStream(new File(xmlFile));
			SAXReader reader = new SAXReader();
			Document doc = reader.read(fis);
			Element root = doc.getRootElement();
			Element notif = root.element("notifications");

			String outString = "Device = $\n\n";// output text
			fos.write(outString.getBytes());
			fos.flush();
			// get all notifications
			for (Iterator j = notif.elementIterator("notification"); j
					.hasNext();) {
				Element obj = (Element) j.next();

				outString = "";
				outString += "oid \"" + obj.attributeValue("oid") + "\" = {\n";
				outString += "\t@severity = \n\t\n";
				int i = 1;
				StringBuilder alertKey = new StringBuilder("");
				// String alertKey = "";

				// get all trap object
				for (Iterator k = obj.element("objects").elementIterator(
						"object"); k.hasNext(); i++) {
					Element module = (Element) k.next();
					String name = module.attributeValue("name");
					outString += "\t$" + name + " = $" + i + "\n";
					alertKey.append("$").append(name).append("+");
					// alertKey += "$" + name + "+";
				}
				// trap has no object
				if (alertKey.length() == 0)
					alertKey = new StringBuilder("no key neeeded");
				// alertKey = "no key neeeded";
				else
					alertKey.deleteCharAt(alertKey.length() - 1);

				String agentGroup = obj.attributeValue("name");
				String summary = "\n";
				outString += "\n\t$OS_EventId = " + mibName + "-" + agentGroup
						+ "\n";

				// parse to rule info
				RuleInfo ri = new RuleInfo();
				ri.setAgent(mibName);
				ri.setAgentGroup(agentGroup);
				ri.setAlertKey(alertKey.toString());
				ri.setSummary(summary);

				outString += ri.toString() + "}\n\n";
				// System.out.println(outString);

				fos.write(outString.getBytes());
				fos.flush();
			}
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void createFile() throws IOException {
		file = new File(outputFile);
		if (!file.exists()) {
			file.createNewFile();
		}
		fos = new FileOutputStream(file);
	};

	public static void main(String args[]) {
		if (args.length == 1 && args[0].equals("-h")) {
			System.out
					.println("java com.sanss.trapParse.Xml2Rules <mibDir> <ruleDir> <mibName>\nmibDir: path of the folder contains xml format mib\nruleDir: path of the folder to put rules file\nmibName: Mib module name without '.xml' surfix");
		} else if (args.length == 3) {
			String mibDir = args[0];
			String ruleDir = args[1];
			String mibName = args[2];
			Xml2Rules x2r = new Xml2Rules(mibDir, ruleDir, mibName);
			x2r.NotificationParser();
		} else
			System.out.println("use '-h' for help");
	}

}
